package com.zznode.redsun.smartcrm.business.action;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.zznode.csp.domain.commons.persistence.EntityManager;
import com.zznode.csp.domain.commons.query.Page;
import com.zznode.redsun.commons.dto.QueryParameter;
import com.zznode.redsun.commons.utils.AbstractBaseAction;
import com.zznode.redsun.commons.utils.GlobalUtils;
import com.zznode.redsun.smartcrm.business.service.ProviderService;
import com.zznode.redsun.smartcrm.model.Provider;

@Controller
@Namespace("/business/provider")
public class ProviderAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	private EntityManager entityManager;
	@Autowired
	private ProviderService providerService;

	private Page<JSONObject> pageData;
	private Provider provider;
	private QueryParameter params;

	@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp") })
	public String index() {
		return SUCCESS;
	}

	@Action(value = "list", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = { "root", "pageData",
			"excludeNullProperties", "true" }) })
	public String list() {
		this.pageData = this.providerService.query(params, getPageNumber(), getPageSize());
		return SUCCESS;
	}

	@Action(value = "addNew", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String addNew() {
		this.provider = new Provider();
		this.provider.setCode(GlobalUtils.generateCode(null));
		return SUCCESS;
	}

	@Action(value = "edit.jsp", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String edit() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			this.provider = this.entityManager.get(Provider.class, id);
		} else {
			return addNew();
		}
		return SUCCESS;
	}

	@Action(value = "save", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String save() {
		if (null == this.provider) {
			this.setJsonMessage("保存失败，提交数据为空.");
		} else {
			if (null == this.provider.getId()) {
				this.entityManager.save(provider);
			} else {
				this.entityManager.update(provider);
			}
		}
		return SUCCESS;
	}

	@Action(value = "remove", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String remove() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			this.entityManager.deleteById(Provider.class, Long.valueOf(id));
		} else {
			this.setJsonMessage("删除失败，提交数据为空!");
		}
		return SUCCESS;
	}

	public Page<JSONObject> getPageData() {
		return pageData;
	}

	public void setPageData(Page<JSONObject> pageData) {
		this.pageData = pageData;
	}

	public Provider getProvider() {
		return provider;
	}

	public void setProvider(Provider provider) {
		this.provider = provider;
	}

	public QueryParameter getParams() {
		return params;
	}

	public void setParams(QueryParameter params) {
		this.params = params;
	}

}
